package com.msb.mall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.msb.common.exception.groups.AddGroupsInterface;
import com.msb.common.exception.groups.UpdateGroupsInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.product.entity.BrandEntity;
import com.msb.mall.product.service.BrandService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author xxf
 * @email 1983752950@qq.com
 * @date 2022-09-10 22:42:29
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/all")
    public R queryAllBrand(){

        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName("习先锋");
        return R.ok().put("lll",brandEntity);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * 通过@Valid注解来开启JSR303的校验

    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand, BindingResult result){

        if (result.hasErrors()){
        // 提交的数据经过JSR303校验后有非法的字段
        HashMap<String, String> map = new HashMap<>();

        List<FieldError> fieldErrors = result.getFieldErrors();

            for (FieldError fieldError : fieldErrors) {
                // 获取非法数据的 field
                String field = fieldError.getField();
                // 获取非法的field的提示信息,@NotBlank(message = "hhhh")
                String defaultMessage = fieldError.getDefaultMessage();
                map.put(field,defaultMessage);
            }
                return R.error(400,"提交的数据不规范").put("data",map);
        }
		brandService.save(brand);

        return R.ok();
    }*/


    /**
     * 信息
     * @param brand
     * @return
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroupsInterface.class) @RequestBody BrandEntity brand) {

        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update( @Validated(UpdateGroupsInterface.class) @RequestBody BrandEntity brand){
		//brandService.updateById(brand);
        brandService.updateDetali(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
